﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace yq.Exchange
{

    /// <summary>
    /// 内部对象 用于通过wcf创建对象
    /// </summary>
   internal static class ChannelFactoryCreator
      {
          /// <summary>
          /// 对象缓存
          /// </summary>
          private static Hashtable channelFactories = new Hashtable();
   

          /// <summary>
          /// wcf创建对象
          /// </summary>
          /// <typeparam name="T">创建对象类型</typeparam>
          /// <param name="endpointName">代理名称</param>
          /// <returns></returns>
          public static ChannelFactory<T> Create<T>(string endpointName)
          {
              if (string.IsNullOrEmpty(endpointName))
              {
                  throw new ArgumentNullException("endpointName");
              }

              //这里根本不需要缓存运行效率并不会改变
              //ChannelFactory<T> channelFactory = null;
              //if(channelFactories.ContainsKey(endpointName))
              //{
              //    //检测缓存有没有 有就输出
              //    channelFactory = channelFactories[endpointName] as ChannelFactory<T>;
              //}
   
              //if (channelFactory == null)
              //{
              //    //调用系统创建对象的方法
              //    channelFactory = new ChannelFactory<T>(ChannelFactoryCreator.Bind, endpointName);
              //    lock (channelFactories.SyncRoot)
              //    {
              //        //写入到缓存
              //        channelFactories[endpointName] = channelFactory;
              //    }
              //}
              //return channelFactory;

              return new ChannelFactory<T>(ChannelFactoryCreator.Bind, endpointName);
          }

          private static WSHttpBinding _Bind = null;
          /// <summary>
          /// 服务配置--需要和服务端配置一样
          /// </summary>
          private static WSHttpBinding Bind
          {
              get
              {

                  WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);
                 binding.SendTimeout = new TimeSpan(0, 10, 10);
                 binding.OpenTimeout = new TimeSpan(0, 10, 0);
                 binding.CloseTimeout = new TimeSpan(0, 10, 0);
                  binding.MaxBufferPoolSize = 2048000000L;
                  binding.MaxReceivedMessageSize = 2048000000L;
                  binding.TextEncoding = Encoding.UTF8;
                  binding.MessageEncoding = WSMessageEncoding.Mtom;
                  binding.BypassProxyOnLocal = true;
                  binding.AllowCookies = true;
                  binding.ReaderQuotas.MaxStringContentLength = 2048000000;
                  binding.ReaderQuotas.MaxArrayLength = 2048000000;
                  binding.ReaderQuotas.MaxBytesPerRead = 2048000000;
                  binding.ReaderQuotas.MaxDepth = 64;
               
                  ChannelFactoryCreator._Bind = binding;
                  return ChannelFactoryCreator._Bind;
              }
          }
      }
}
